package cn.jiyun.service.impl;

import cn.jiyun.entity.XmAudit;
import cn.jiyun.entity.XmUser;
import cn.jiyun.mapper.XmAuditMapper;
import cn.jiyun.mapper.XmUserMapper;
import cn.jiyun.service.IXmAuditService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-11-21
 */
@Service
public class XmAuditServiceImpl extends ServiceImpl<XmAuditMapper, XmAudit> implements IXmAuditService {



    @Autowired
    private XmUserMapper xmUserMapper;
    @Override
    public Page<XmAudit> findAll(Integer current, Integer pageSize, XmAudit xmAudit) {
        Page<XmAudit> xmAuditPage = new Page<>(current, pageSize);
        QueryWrapper<XmAudit> xmAuditQueryWrapper = new QueryWrapper<>();
        if (xmAudit.getSerialNumber()!=null){
            xmAuditQueryWrapper.like("serial_number",xmAudit.getSerialNumber());
        }
        if(xmAudit.getId()!=null){
            xmAuditQueryWrapper.like("uid",xmAudit.getId());
        }
        if(xmAudit.getUname()!=null &&!xmAudit.getUname().equals("")){
            xmAuditQueryWrapper.like("uname",xmAudit.getUname());
        }
        if(xmAudit.getMobilePhone()!=null && !xmAudit.getMobilePhone().equals("")){
            xmAuditQueryWrapper.like("mobile_phone",xmAudit.getMobilePhone());
        }
        if(xmAudit.getAudit()!=null){
            xmAuditQueryWrapper.eq("audit",xmAudit.getAudit());
        }
         if (xmAudit.getZtime() != null) {
            LocalDateTime now = LocalDateTime.now();
            int mo = 90;
            if (xmAudit.getZtime() == 1) {
              xmAuditQueryWrapper.apply("ABS(DATEDIFF({0}, submission_time)) <= {1}", now, mo);
            }

            if(xmAudit.getZtime()==2){
                xmAuditQueryWrapper.apply("YEAR(submission_time)=YEAR(CURRENT_DATE)");
            }

            if(xmAudit.getZtime()==3){
                xmAuditQueryWrapper.apply("DATE_FORMAT(submission_time,'%Y')=2022");
            }

            if(xmAudit.getZtime()==4){
                 xmAuditQueryWrapper.apply("DATE_FORMAT(submission_time,'%Y')=2021");
            }
             if(xmAudit.getZtime()==5){
                 xmAuditQueryWrapper.apply("DATE_FORMAT(submission_time,'%Y')=2020");
            }
              if(xmAudit.getZtime()==6){
                 xmAuditQueryWrapper.apply("DATE_FORMAT(submission_time,'%Y')<2020");
            }
        }

         if(xmAudit.getStatus()!=null){
             xmAuditQueryWrapper.eq("status",xmAudit.getStatus());
         }

         if(xmAudit.getReviewers()!=null && !xmAudit.getReviewers().equals("")){
             xmAuditQueryWrapper.like("reviewers",xmAudit.getReviewers());
         }
        return  baseMapper.selectPage(xmAuditPage,xmAuditQueryWrapper);
    }


    @Override
    public void xmAudit(XmAudit xmAudit) {
        xmAudit.setReviewTime(LocalDateTime.now());
        if(xmAudit.getStatus()==3){
            XmUser xmUser = new XmUser();
            xmUser.setId(xmAudit.getUid());
            if(xmAudit.getAudit()==1){
                xmUser.setImg(xmAudit.getNewInformation());
            }else if(xmAudit.getAudit()==2){
                xmUser.setName(xmAudit.getNewInformation());
            }
            xmUserMapper.updateById(xmUser);
        }
        baseMapper.updateById(xmAudit);
    }
}
